package com.example.fengyungo.activity.main;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.fengyungo.R;
import com.example.fengyungo.activity.BaseActivity;
import com.example.fengyungo.adapter.OrderAdapter;
import com.example.fengyungo.constant.DriverConstant;
import com.example.fengyungo.constant.ResultConstant;
import com.example.fengyungo.domain.Order;
import com.example.fengyungo.domain.OrderDetailsVO;
import com.example.fengyungo.utils.OkHttpUtils;
import com.example.fengyungo.utils.ThreadPoolUtil;

import java.util.List;

/**
 * 搜索界面
 *
 * @author XLR
 */
public class SearchActivity extends BaseActivity {

    private static final String TAG = "SearchActivity";

    private EditText beginEt;

    private EditText endEt;

    private ProgressBar progressBar;

    private RecyclerView recyclerView;
    /**
     * LinearLayoutManager
     */
    private LinearLayoutManager layoutManager;
    /**
     * OrderAdapter
     */
    private OrderAdapter adapter;
    /**
     * 司机可接的订单
     */
    private List<OrderDetailsVO> orders;

    private String begin;

    private String end;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        beginEt = findViewById(R.id.et_begin);
        endEt = findViewById(R.id.et_end);

        ImageButton backBtn = findViewById(R.id.back);
        ImageView search = findViewById(R.id.search);

        progressBar = findViewById(R.id.progress_bar);

        recyclerView = findViewById(R.id.recycler_view);
        layoutManager = new LinearLayoutManager(this);

        search.setOnClickListener((v) -> {
            begin = beginEt.getText().toString();
            end = endEt.getText().toString();
            // 根据起点或终点查询司机可接的订单
            getAccessByBeginOrEnd();
        });

        backBtn.setOnClickListener(view -> finish());
    }

    /**
     * 根据起点或终点查询司机可接的订单
     */
    private void getAccessByBeginOrEnd() {
        progressBar.setVisibility(View.VISIBLE);
        Runnable runnable = () -> {
            try {
                Order order = new Order();
                order.setBegin(begin);
                order.setEnd(end);
                String json = JSONObject.toJSONString(order);
                // 发送GET请求到后端，并接收响应数据
                String url = DriverConstant.URL_USER_DRIVER + "/beginOrEnd";
                String responseData = OkHttpUtils.post(url, json);

                JSONObject jsonObject = JSONObject.parseObject(responseData);
                String data = jsonObject.getString(ResultConstant.DATA);
                orders = JSON.parseArray(data, OrderDetailsVO.class);
                Log.d(TAG, "getAccessByBeginOrEnd: orders:"+orders);

                runOnUiThread(() -> {
                    progressBar.setVisibility(View.GONE);
                    recyclerView.setLayoutManager(layoutManager);
                    adapter = new OrderAdapter(orders);
                    recyclerView.setAdapter(adapter);
                });

            } catch (Exception e) {
                e.printStackTrace();
                runOnUiThread(() -> Toast.makeText(SearchActivity.this,
                        getString(R.string.connection_error), Toast.LENGTH_SHORT).show());
            }
        };
        ThreadPoolUtil.execute(runnable);
    }
}